package com.frank.code_notes.C01_array;

import cn.hutool.core.lang.Console;

/**
 * @Description 69.x 的平方根
 * @Author zhoujr
 * @Data 2024/11/25 13:57
 */
public class L069SquareRootOfX {

    /**
     * @Description 使用二分查找
     */
    public static int mySqrt(int x) {
        int left = 0, right = x, ans = -1;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if ((long) mid * mid > x) {
                right = mid - 1;
            } else {
                ans = mid;
                left = mid + 1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Console.log(mySqrt(4));
    }

}
